﻿using WorldmeServer.Attributes;
using WorldmeServer.Models.Base;
using WorldmeServer.Models.材质相关;
using WorldmeServer.Services.Database;
using Microsoft.EntityFrameworkCore;

namespace WorldmeServer.Services.Business.衣柜相关
{
    [ScopedRegistration]
    public class ClosetService
    {
        private readonly MySqlContext mysql;
        public ClosetService(MySqlContext mysql)
        {
            this.mysql = mysql;
        }

        public async Task<ServiceResult<List<Texture>>> GetTextures(string userId, string type)
        {
            var userClothes = await mysql.Closets
                .AsNoTracking()
                .Where(c => c.UserId == userId)
                .Join(mysql.Textures, c => c.TextureId, t => t.Id, (c, t) => t)
                .Where(t => type == "skin" ? (t.Type == TextureType.Steve || t.Type == TextureType.Alex) : (t.Type == TextureType.Cape))
                .ToListAsync();
            return ServiceResult<List<Texture>>.Success(userClothes);
        }

        public async Task<ServiceResult<Texture>> AddTexture(string userId, string textureId)
        {
            throw new NotImplementedException();
        }

        public async Task<ServiceResult<Boolean>> HasTexture(string userId, string textureId)
        {
            var has = await mysql.Closets.AnyAsync(c => c.UserId == userId && c.TextureId == textureId);
            return ServiceResult<Boolean>.Success(has);
        }

        public async Task<ServiceResult<Boolean>> DeleteTexture(string userId, string textureId)
        {
            var has = await mysql.Closets.AnyAsync(c => c.UserId == userId && c.TextureId == textureId);
            return ServiceResult<Boolean>.Success(has);
        }
    }
}
